﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProdeManagmente.DAO;
using ProdeManagmente.Model;
using DgvFilterPopup;
using ProdeManagmente.Libs;

namespace ProdeManagmente.Forms.ABMS
{
    public partial class FrmListTeam : Form
    {
        private static FrmListTeam instance;
        private TeamService TeamService;

        private FrmListTeam()
        {
            InitializeComponent();
            LoadForm();
        }

        public static FrmListTeam getFrmListTeamInstance()
        {
            if (instance == null)
            {
                instance = new FrmListTeam();
            }
            instance.Select();
            return instance;
        }

        private void LoadForm()
        {
            List<Team> teamsList = new List<Team>();
            TeamService = TeamService.getTeamServiceInstance();
            teamsList = TeamService.getTeamListFilter("");


            DataTable dataTable = new DataTable();
            dataTable.Columns.AddRange(new DataColumn[3] {
                                new DataColumn("ID",typeof(int)),
                                new DataColumn("EQUIPO",typeof(string)),
                                new DataColumn("LIGA",typeof(string))});

            foreach (Team team in teamsList)
            {
                dataTable.Rows.Add(team.getId(), team.getName(), team.getLeague());
            }

            dataGridView1.DataSource = dataTable;
            DgvFilterManager filterManager = new DgvFilterManager(dataGridView1);
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            FrmTeam frmTeam = new FrmTeam(null);
            frmTeam.ShowDialog();
            LoadForm();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            long idTeam = Convert.ToInt64(dataGridView1.SelectedRows[0].Cells[0].Value.ToString());

            Team tournament = TeamService.getTeamById(idTeam);
            FrmTeam frmTeam = new FrmTeam(tournament);
            frmTeam.ShowDialog();
            LoadForm();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Desea borrar el elemento?", "Salir", MessageBoxButtons.YesNo);

            if (result == DialogResult.Yes)
            {
                try
                {
                    long idTeam = Convert.ToInt64(dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
                    TeamService.delete(idTeam);
                }
                catch (BusinessException error)
                {
                    new MensajeSalida(error).showMensaje();
                }

                LoadForm();
            }
        }

        private void FrmListTeam_FormClosed(object sender, FormClosedEventArgs e)
        {
            instance = null;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            long idTeam = Convert.ToInt64(dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
            Team tournament = TeamService.getTeamById(idTeam);

            FrmTeamsTournament frmTeamsTournament = new FrmTeamsTournament(tournament);
            frmTeamsTournament.ShowDialog();
        }
    }
}
